package com.finalab.website.util;



import javax.validation.*;
import java.util.Set;
public class BeanValidatorUtil {

    private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();



    /**
     * 验证某个bean的参数
     *
     * @param object 被校验的参数
     */
    public static <T> boolean validate(T object,Class ... groups) {
        //获得验证器
        //执行验证
        Set<ConstraintViolation<T>> constraintViolations = validatorFactory.getValidator().validate(object, groups);
        //如果有验证信息，则将第一个取出来包装成异常返回
        if(constraintViolations != null && constraintViolations.size() > 0){
            return false;
        }
        return true;
    }
}
